package com.heima.listener;

import com.alibaba.fastjson.JSONObject;
import com.heima.utils.ConstantUtil;
import com.heima.utils.HttpRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class WxTokenListener {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value(value = "${vxplate.appid}")
    private String appid;

    @Value(value = "${vxplate.secret}")
    private String secret;
    //定时从刷新微信token并存放在redis中
    //@Scheduled(cron = "* * 0/2 * * ? ")
    public  void  getAccess_token() {

        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        url = url.replace("APPID", appid).replace("APPSECRET", secret);
        String access_token = HttpRequestUtils.httpGet(url);
        System.out.println("access_token:"+access_token);
        //将json字符串转化成map，从map中获取access_token的值
        Map access_tokenToMap =  JSONObject.parseObject(access_token);
//将access_token存放在redis中
        stringRedisTemplate.opsForValue().set(ConstantUtil.ACCESS_TOKEN,access_tokenToMap.get("access_token").toString());
    }



}
